package com.clkj.zhonghui.controller.main;

import com.clkj.zhonghui.pojo.common.Result;
import com.clkj.zhonghui.pojo.common.StatusCode;
import com.clkj.zhonghui.pojo.order.Cart;
import com.clkj.zhonghui.service.main.MainService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author yawang
 * @create 2024/07/16 14:29:58
 **/

@RestController
@RequestMapping(value = "/main")
@Api(value = "/main", tags = {"商城首页"})
@CrossOrigin
public class MainController {
    @Autowired
    private MainService mainService;
    @PostMapping("/listMain")
    @ApiOperation(value = "app-商城首页")
    public Result listMain(Integer shopId){
        try {
            Map map = mainService.listMain(shopId);
            return new Result(true, StatusCode.OK,"查询成功",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,"查询失败");
        }
    }

    @PostMapping("/addCart")
    @ApiOperation(value = "app-加入购物车")
    public Result addCart(Cart cart){
        try {
            mainService.addCart(cart);
            return new Result(true, StatusCode.OK,"添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,"查询失败");
        }
    }

    @PostMapping("/listUserCart")
    @ApiOperation(value = "app-获取我的购物车")
    public Result listUserCart(Integer userId){
        try {
            List<Cart> carts = mainService.listUserCart(userId);
            return new Result(true, StatusCode.OK,"查询成功",carts);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,"查询失败");
        }
    }

    @PostMapping("/delCart")
    @ApiOperation(value = "删除购物车商品")
    public Result delCart(String ids){
        try {
            mainService.delCart(ids);
            return new Result<>(true, StatusCode.OK, "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result<>(false, StatusCode.ERROR, "删除失败");
        }
    }

}
